package Demo.mapper;

import Demo.pojio.User;
import Demo.util.DBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private DBUtil dbUtil;
    @Override
    public int insertUser(User user) {
        System.out.println("UserMapper:插入用户数据...");
//        Connection connection = dbUtil.getConnection();
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "INSERT INTO user(username,password,nickname,age) " +
                    "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
            return ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
